//
//  WorkdayViewController.m
//  999-考勤定位
//
//  Created by 李晓东 on 2018/4/8.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "WorkdayViewController.h"

@interface WorkdayViewController ()
@property (nonatomic, strong) NSMutableDictionary *dict;

@end

@implementation WorkdayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dict = [NSMutableDictionary dictionary];
    
    for (int i = 0; i < 7; i++) {
        _dict[[NSString stringWithFormat:@"%d", i]] = [NSNumber numberWithInt:0];
    }
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"---%@----", _chosenDayStr);
    NSArray *weekdays = [_chosenDayStr componentsSeparatedByString:@" "];
    NSIndexPath *indexPath;
    for (int i = 0; i < weekdays.count; i++) {
        if ([weekdays[i] isEqualToString:@"日"]) {
            indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            _dict[@"0"] = @1;
        } else if ([weekdays[i] isEqualToString:@"一"]) {
            indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            _dict[@"1"] = @2;
        } else if ([weekdays[i] isEqualToString:@"二"]) {
            indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
            _dict[@"2"] = @3;
        } else if ([weekdays[i] isEqualToString:@"三"]) {
            indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
            _dict[@"3"] = @4;
        } else if ([weekdays[i] isEqualToString:@"四"]) {
            indexPath = [NSIndexPath indexPathForRow:4 inSection:0];
            _dict[@"4"] = @5;
        } else if ([weekdays[i] isEqualToString:@"五"]) {
            indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
            _dict[@"5"] = @6;
        } else if ([weekdays[i] isEqualToString:@"六"]) {
            indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
            _dict[@"6"] = @7;
        }
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

- (IBAction)confirmClick:(id)sender {
// 以下代码仅为测试
//    NSArray *array = @[@"1", @"2"];
//    NSArray *arraykey = @[[NSString stringWithFormat:@"ding"], [NSString stringWithFormat:@"ding2"]];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"Workday" object:self userInfo:[NSDictionary dictionaryWithObjects:array forKeys:arraykey]];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Workday" object:self userInfo:_dict];
    [self dismissViewControllerAnimated:YES completion:nil];
    // 这是为了复习建新线程
//    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _dict[[NSString stringWithFormat:@"%ld", indexPath.row]] = [NSNumber numberWithInteger:indexPath.row + 1];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *btn = [cell viewWithTag:indexPath.row + 1];
    [btn setSelected:YES];
    NSLog(@"done");
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    _dict[[NSString stringWithFormat:@"%ld", indexPath.row]] = [NSNumber numberWithInt:0];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *btn = [cell viewWithTag:indexPath.row + 1];
    [btn setSelected:NO];
}

@end
